<template>
  <div class="login">
    <div class="header">
      <span class="header-logo">
        <i class="iconfont icon-vue"></i>
        <i class="iconfont icon-icon-test"></i>
        <i class="iconfont icon-typescript"></i>
      </span>
      <span class="header-title">在线考勤系统</span>
    </div>
    <div class="desc">
      零基础从入门到进阶，系统掌握前端三大热门技术(Vue、React、TypeScript)
    </div>
    <el-form :model="ruleForm" :rules="rules" label-width="100px" class="main">
      <el-form-item label="邮箱" prop="email">
        <el-input
          type="text"
          v-model="ruleForm.email"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
    <div class="users">
      <el-row :gutter="20">
        <el-col :span="12" v-for="item in testUser">
          <h3>
            测试账号，<el-button @click="autoLogin(item)">一键登录</el-button>
          </h3>
          <p>邮箱:{{ item.email }}</p>
          <p>密码：{{ item.pass }}</p>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script setup lang="ts">
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";
import { useStore } from "@/store";
import router from "@/router";
interface User {
  email: string;
  pass: string;
}
const store = useStore();
const ruleFormRef = ref<FormInstance>();
// 校验
const rules = reactive<FormRules>({
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
  ],
  pass: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
// 登录收集的表单
const ruleForm = reactive<User>({
  email: "",
  pass: "",
});
// 登录按钮
const submitForm = (formEl: FormInstance | undefined) => {
  store.dispatch("users/login", ruleForm).then((res) => {
    if (res.data.errcode == 0) {
      console.log("res");

      ElMessage.success("登录成功");
      store.commit("users/updateToken", res.data.token);
      router.push("/");
    } else {
      ElMessage.error("登录失败");
    }
  });
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      store.dispatch("users/login", ruleForm).then((res) => {
        console.log(res.data);
      });
    } else {
      console.log("error submit!");
      return false;
    }
  });
};
//测试登录用户信息
const testUser: User[] = [
  {
    email: "huangrong@imooc.com",
    pass: "huangrong",
  },
  {
    email: "huangrong@imooc.com",
    pass: "huangrong",
  },
];
//一键登录
const autoLogin = (user: User) => {
  ruleForm.email = user.email;
  ruleForm.pass = user.pass;
  submitForm(ruleFormRef.value);
};
</script>
<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("@/assets/images/login-bg.svg") no-repeat center 110px;
  background-size: 100%;

  .header {
    height: 44px;
    line-height: 44px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 34px;
    padding-top: 100px;
    border: 1px solid #ddd;

    .header-logo {
      .icon-vue,
      .icon-icon-test,
      .icon-typescript {
        margin-right: 5px;
        font-size: inherit;
      }

      .icon-vue {
        color: green;
      }

      .icon-icon-test {
        color: #deb887;
      }

      .icon-typescript {
        color: blue;
      }
    }

    .header-title {
      margin-left: 30px;
      font-weight: 700;
      font-size: 30px;
    }
  }

  .desc {
    text-align: center;
    padding-top: 30px;
    color: rgba(0, 0, 0, 0.45);
    font-size: 16px;
  }

  .main {
    width: 500px;
    margin: 0 auto;
    padding-top: 50px;
    border: 1px solid darkblue;
  }

  .users {
    width: 500px;
    margin: 60px auto;
  }

  color: rgba(0, 0, 0, 0.65);

  h3 {
    font-size: 16px;
  }

  p {
    margin: 20px;
  }
}
</style>
